package com.dobby.xmlrpc.demo.metablog;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jonas on 2017/1/11.
 */
public class SinaBlogPoster {


    public static String newPost(String apiUrl, String title, String content, String userName, String password) throws MalformedURLException, XmlRpcException {

        XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
        clientConfig.setServerURL(new URL(apiUrl));
        XmlRpcClient blogClient = new XmlRpcClient();
        blogClient.setConfig(clientConfig);

        Map<String, Object> post = new HashMap<String, Object>();
        post.put("title", title);
        post.put("description", content);
        Object[] params = new Object[]{"_blogid", userName, password, post, Boolean.TRUE};
        String result = (String) blogClient.execute("metaWeblog.newPost", params);
        return result;
    }



    public static String editPost(String apiUrl, String postId, String title, String content, String userName, String password) throws MalformedURLException, XmlRpcException {

        XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
        clientConfig.setServerURL(new URL(apiUrl));
        XmlRpcClient blogClient = new XmlRpcClient();
        blogClient.setConfig(clientConfig);

        Map<String, Object> post = new HashMap<String, Object>();
        post.put("title", title);
        post.put("description", content);
        Object[] params = new Object[]{postId, userName, password, post, Boolean.TRUE};
        String result = (String) blogClient.execute("metaWeblog.editPost", params);
        return result;
    }




    public static Boolean deletePost(String apiUrl, String postId, String userName, String password) throws MalformedURLException, XmlRpcException {

        XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
        clientConfig.setServerURL(new URL(apiUrl));
        XmlRpcClient blogClient = new XmlRpcClient();
        blogClient.setConfig(clientConfig);

        Object[] params = new Object[] {"appKey", postId, userName, password, Boolean.TRUE};
        Boolean result = (Boolean) blogClient.execute("blogger.deletePost", params);
        return result;

    }


    public static Boolean existBlog(String apiUrl, String userName, String password) throws MalformedURLException, XmlRpcException {
        XmlRpcClientConfigImpl clientConfig = new XmlRpcClientConfigImpl();
        clientConfig.setServerURL(new URL(apiUrl));
        XmlRpcClient blogClient = new XmlRpcClient();
        blogClient.setConfig(clientConfig);

        Object[] params = new Object[] {"appKey", userName, password};
        Object object = blogClient.execute("blogger.getUsersBlogs", params);
        if(null == object) {
            return false;
        }
        return true;
    }

}
